package cn.wendellup.bootStrap.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import cn.wendellup.bootStrap.dao.contract.IAdvDao;
import cn.wendellup.bootStrap.valueobject.entity.AdvDetail;
import cn.wendellup.bootStrap.valueobject.entity.AdvPosition;
import cn.wendellup.bootStrap.valueobject.entity.AdvPositionLink;

@Repository
public class AdvDaoImpl implements IAdvDao {

	private static Logger logger = Logger.getLogger(AdvDaoImpl.class);

	@Autowired
	private JdbcTemplate jdbcTemplate;

	private static final String STR_SELECT_USER = " id, firstname, lastname, phone, email ";
	private static final String STR_SELECT_ADVDETAIL = 
			" id, position_id, ad_type, record_type, efs_id, operator_id"
			+ " , operation_name, link_url, status, sort_no, description"
			+ " , begin_time, end_time, insert_time, update_time, title ";
	private static final String STR_SELECT_ADVPOSITIONLINK = 
			" adv_position_id, app_id, sort_no, type ";
	private static final String STR_SELECT_ADVPOSITION =
			" id, type_id, name, width, height, price, enable, description"
			+ " ,adv_count ";
	
	@Override
	public List<Integer> listAdvDetailIdsByAdvPostionId(Integer advPositionId) {
		List<Integer> advDetailIds = new ArrayList<Integer>();
		String sql = " select id from t_operation_ad ";
		logger.info(sql);
		List<Map<String, Object>> queryResult = jdbcTemplate.queryForList(sql);
		for (Map<String, Object> map : queryResult) {
			Integer id = (Integer) map.get("id");
			
			advDetailIds.add(id);
		}

		return advDetailIds;
	}

	@Override
	public List<Integer> listAdvPositionIdByAppId(Integer appParameterId) {
//		List<Integer> advDetailIds = new ArrayList<Integer>();
		String sql = " select adv_position_id from t_operation_app_advpos_link "
				+ " where app_id = %1$d ";
		sql = String.format(sql, appParameterId);
		logger.info(sql);
//		List<Map<String, Object>> queryResult = jdbcTemplate.queryForList(sql);
		return jdbcTemplate.queryForList(sql, Integer.class);
//		for (Map<String, Object> map : queryResult) {
//			Integer id = (Integer) map.get("adv_position_id");
//			advDetailIds.add(id);
//		}
//
//		return advDetailIds;
	}

	@Override
	public AdvDetail getAdvDetailById(Integer id) {
		String sqlOrginal = " select "
				+ STR_SELECT_ADVDETAIL
				+ " from t_operation_ad where id = %1$d "; 
		final String sql = String.format(sqlOrginal, id);
		logger.info(sql);
		AdvDetail advDetail = jdbcTemplate.queryForObject(sql, new AdvDetail());
		return advDetail;
	}

	@Override
	public AdvPositionLink getAdvPositionLinkByPositionAndAppId(
			Integer positionId, Integer appId) {
		String sqlOrginal = " select "
				+ STR_SELECT_ADVPOSITIONLINK
				+ " from t_operation_app_advpos_link "
				+ " where adv_position_id = %1$d "
				+ " and app_id = %2$d "; 
		final String sql = String.format(sqlOrginal, positionId, appId);
		logger.info(sql);
		AdvPositionLink advPositionLink = 
				jdbcTemplate.queryForObject(sql, new AdvPositionLink());
		return advPositionLink;
	}

	@Override
	public AdvPosition getAdvPositionById(Integer id) {
		String sqlOrginal = " select "
				+ STR_SELECT_ADVPOSITION
				+ " from t_operation_ad_position where id = %1$d "; 
		final String sql = String.format(sqlOrginal, id);
		logger.info(sql);
		AdvPosition advPosition = jdbcTemplate.queryForObject(sql, new AdvPosition());
		return advPosition;
	}
}
